import clickEvent from "./clickEvent.js";
import {Graph} from "@antv/g6";
import baseOption from './options'
import registerEdge from './custom/customEdge'
import registerEdge1 from './custom/customEdge1'
import {allNodesIds, allEdgesIds} from './store'

let graphExample = null

//初始化图谱方法
class initGraph {
    constructor(config) {
        this.data = config.data
        this.width = config.width
        this.height = config.height
        this.container = config.container
        this.optionsConfig = config.optionsConfig
    }

    // 初始化图谱
    initGraph(callBack) {
        // 注册节点
        registerEdge()
        registerEdge1()
        let example = null
        example = new Graph({
            data: this.data,
            width: this.width,
            height: this.height,
            container: this.container,
            transforms: [{
                type: 'process-parallel-edges',
                key: 'process-parallel-edges',
                mode: 'bundle',
                distance: 32,
            }],
            ...baseOption(this.optionsConfig)
        });
        example.render().then(() => {
            callBack ? callBack(example) : null
            if (this.optionsConfig.bindNodeClick) {
                const graphClickEvent = new clickEvent(example, this.optionsConfig)
                graphClickEvent.init()
            }
            // 赋值给全局变量
            graphExample = example
            // 获取所有节点id
            allNodesIds.value = this.getNodesIds()
            // 获取所有边的id
            allEdgesIds.value = this.getEdgesIds()
        })
    }

    getNodesIds() {
        return graphExample?.getNodeData().map(item => item.id)
    }

    getEdgesIds() {
        return graphExample?.getEdgeData().map(item => item.id)
    }
}

export {initGraph, graphExample}
